package com.api.shopapi.controller;


import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.service.ESeeDoctorRecordService;
import com.api.shopapi.service.GovernmentService;
import com.api.shopapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("ESeeDoctorRecord")
public class ESeeDoctorRecordController extends BaseController{

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @Autowired
    @Qualifier("ESeeDoctorRecordServiceImplOne")
    private ESeeDoctorRecordService seeDoctorRecordService;

    @Autowired
    @Qualifier("GovernmentServiceImplOne")
    private GovernmentService governmentService;

    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String userCode = jsonObject.getString("userCode");
            String elderlyCode = jsonObject.getString("elderlyCode");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/ESeeDoctorRecord/Add");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            ESeeDoctorRecord eSeeDoctorRecord = seeDoctorRecordService.getByElderlyCode(elderlyCode);
            if (eSeeDoctorRecord!=null){
                seeDoctorRecordService.delete(eSeeDoctorRecord.getESeeDoctorRecordCode());
            }

            User user = userService.getByUserCode(userCode);

            String hospital = jsonObject.getString("hospital");
            String department = jsonObject.getString("department");
            String doctor = jsonObject.getString("doctor");
            Date seeDoctorTime = jsonObject.getDate("seeDoctorTime");
            Date furtherTime = jsonObject.getDate("furtherTime");
            String chiefComplaint = jsonObject.getString("chiefComplaint");
            String physicalExamination = jsonObject.getString("physicalExamination");
            String realityExamination = jsonObject.getString("realityExamination");
            String auxiliaryExamination = jsonObject.getString("auxiliaryExamination");
            String useMedication = jsonObject.getString("useMedication");
            String diagnosisResults = jsonObject.getString("diagnosisResults");
            String treatmentPrescription = jsonObject.getString("treatmentPrescription");
            String aboutTaboo = jsonObject.getString("aboutTaboo");

            ESeeDoctorRecord seeDoctorRecord=new ESeeDoctorRecord();
            seeDoctorRecord.setESeeDoctorRecordCode(UUID.randomUUID().toString());
            seeDoctorRecord.setUserCode(userCode);
            seeDoctorRecord.setElderlyCode(elderlyCode);
            seeDoctorRecord.setElderlyName(user.getName());
            seeDoctorRecord.setHospital(hospital);
            seeDoctorRecord.setDepartment(department);
            seeDoctorRecord.setDoctor(doctor);
            seeDoctorRecord.setSeeDoctorTime(seeDoctorTime);
            seeDoctorRecord.setFurtherTime(furtherTime);
            seeDoctorRecord.setChiefComplaint(chiefComplaint);
            seeDoctorRecord.setPhysicalExamination(physicalExamination);
            seeDoctorRecord.setRealityExamination(realityExamination);
            seeDoctorRecord.setAuxiliaryExamination(auxiliaryExamination);
            seeDoctorRecord.setUseMedication(useMedication);
            seeDoctorRecord.setDiagnosisResults(diagnosisResults);
            seeDoctorRecord.setTreatmentPrescription(treatmentPrescription);
            seeDoctorRecord.setAboutTaboo(aboutTaboo);
            seeDoctorRecord.setStaffCode(staffCode);
            seeDoctorRecord.setGovernmentCode(user.getGovernmentCode());
            seeDoctorRecord.setCreateTime(new Date());

            seeDoctorRecordService.insert(seeDoctorRecord);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ESeeDoctorRecord/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "GetByElderlyCode",method = RequestMethod.POST)
    public MyResult GetByElderlyCode(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String elderlyCode = jsonObject.getString("elderlyCode");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/ESeeDoctorRecord/GetByElderlyCode");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            ESeeDoctorRecord seeDoctorRecord = seeDoctorRecordService.getByElderlyCode(elderlyCode);
            return success(seeDoctorRecord);
        } catch (Exception e) {
            insertError("/ESeeDoctorRecord/GetByElderlyCode",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "GetElderlyInfo",method = RequestMethod.POST)
    public MyResult GetElderlyInfo(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String userCode = jsonObject.getString("userCode");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/ESeeDoctorRecord/GetElderlyInfo");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }
            User user = userService.getByUserCode(userCode);

            JSONObject jsonObject1 =new JSONObject();
            jsonObject1.put("elderlyName",user.getName());
            jsonObject1.put("elderlyPhone",user.getPhone());
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ESeeDoctorRecord/GetElderlyInfo",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

}
